package com.application.Tree;

import java.util.ArrayList;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.view.View;

public class DetailNode{
	int width=210;
	int heigh=60;
	float x=0;
	float y=0;
	TreeNode node;
	public DetailNode(){
		this.node=null;
	}
	public DetailNode (TreeNode tNode){
		this.node=tNode;
		if(tNode!=null){
			this.x=tNode.x-10;
			this.y=tNode.y-10;
		}
	}
	public void onDraw(Canvas canvas){
		if(this.node!=null){
			ArrayList<String> lines = Split(node.text, 38);
			if (lines.size() >= 3)
				heigh = 40 + 15 * lines.size();
			
			Paint p1 = new Paint();
			
//			p1.setARGB(255,200,255,250);//xanh duong ngoc nhat
//			p1.setARGB(255,200,255,200);//xanh la ngoc nhat
//			p1.setARGB(255,140,125,125);//mau nau nhat
//			p1.setARGB(255,220,220,220);//mau xam nhat giong mau hinh nen
//			p1.setARGB(255,173,216,249);//mau xanh duong nhat
			p1.setARGB(255,239,207,249);//mau tim nhat
//			p1.setARGB(255,249,234,207);//mau trung ga nhat
			p1.setAntiAlias(true);
			p1.setStrokeWidth(3);
			
			RectF rect=new RectF(x, y, x + width, y + heigh);
			int space = 1;
			RectF border = new RectF(x - space, y - space, x + width + space, y
					+ heigh + space);
			canvas.drawRoundRect(rect, 10, 10, p1);
			
			p1.setARGB(255,49,3,78);//mau tim dam
			p1.setTextSize(14);
			float t=(float)1.3;
			p1.setTextScaleX(t);
			canvas.drawText(node.getUser(), this.x+5, this.y+12, p1);
			//Vẽ đường gạch ngang
			p1.setStrokeWidth(1);
//		//	p1.setColor(Color.BLUE);
			p1.setARGB(255,94,6,184);//mau tim			
			p1.setTextSize(12);
			p1.setTextScaleX(1);
			

			DashPathEffect dash=new DashPathEffect(new float[]{5,5}, (float)1.0);
			p1.setPathEffect(dash);
			p1.setStyle(Style.STROKE);
			canvas.drawLine(this.x, this.y+20, this.x+width, this.y+20, p1);
			
			Paint p2 = new Paint();
			
			p2.setColor(Color.BLACK);
			p2.setTextSize(12);
			
			p2.setAntiAlias(true);
			p2.setStrokeWidth(2);
			//p2.setStyle(Paint.Style.STROKE);
			for (int i = 0; i < lines.size(); i++) {				
				canvas.drawText(lines.get(i), x + 10, y + 35 + i * 15, p2);
			}
			
			p2.setColor(Color.rgb(56, 93, 138));
			p2.setStyle(Paint.Style.STROKE);
			canvas.drawRoundRect(border, 10, 10, p2);
		}
	}
	
	public void setPosition(float x, float y){
		this.x=x;
		this.y=y;
	}
	public void moveAll(float moveX, float moveY) {
		setPosition(this.x + moveX, this.y + moveY);
	}
	public ArrayList<String> Split(String text, int len) {
		String[] m = text.split(" ");
		String[] line = new String[m.length];
		if (m.length > 0) {
			int n = m.length;
			int j = 0;
			for (int i = 0; i < n; i++) {
				if (m[i].length() >= len) {
					line[j] = m[i];
					j++;
				} else {
					if (line[j] == null) {
						line[j] = m[i];

					} else {
						if ((line[j] + " " + m[i]).length() > len) {
							line[j + 1] = m[i];
							j++;
						} else {
							line[j] += " " + m[i];
							// j++;
						}

					}
				}
			}
		}
		ArrayList<String> rs = new ArrayList<String>();
		for (int i = 0; i < line.length; i++) {
			if (line[i] != null) {
				rs.add(line[i]);
			}
		}
		return rs;
		// return rs.toArray(java.lang.String);
	}
}
